
package game;

import java.io.IOException;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import javafx.event.EventType;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Region;
import javafx.scene.media.AudioClip;
import javafx.scene.transform.Translate;

/**
 * @author lht
 * @time 2020/10/17 22:27
 */
public class GamePane extends Region {

    private ImageView[] iv = new ImageView[4];

    public GamePane() throws IOException {

        ImageView release = Config.getHammers().get(0);
        ImageView presed = Config.getHammers().get(1);
        Translate pos = new Translate(100, 100);
        Translate pp = new Translate(100, 100);

        release.getTransforms().add(pos);
        presed.getTransforms().add(pp);
        presed.setVisible(false);

        ImageView top = loadbackImage("/assets/top.bmp", null, 0);
        ImageView mid_up = loadbackImage("/assets/mid_up.bmp", "/assets/mid_up_y.bmp", -28);
        ImageView mid_down = loadbackImage("/assets/mid_down.bmp", "/assets/mid_down_y.bmp", -37 - 28);
        ImageView bottom = loadbackImage("/assets/bottom.bmp", "/assets/bottom_y.bmp", -50 - 37 - 28);

        getChildren().add(top);
        getChildren().add(Config.little_mouse);
        getChildren().add(mid_up);
        getChildren().add(Config.mid_mouse);
        getChildren().add(mid_down);
        getChildren().add(Config.big_mouse);
        getChildren().add(bottom);

        getChildren().addAll(release, presed);

        iv[0] = top;
        iv[1] = mid_up;
        iv[2] = mid_down;
        iv[3] = bottom;

        AudioClip audioClip = new AudioClip(this.getClass().getResource("/assets/pa.mp3").toExternalForm());

        addEventFilter(MouseEvent.ANY, event -> {
            EventType<? extends MouseEvent> type = event.getEventType();

            if (type == MouseEvent.MOUSE_PRESSED) {
                release.setVisible(false);
                presed.setVisible(true);
                audioClip.play();
            } else if (type == MouseEvent.MOUSE_RELEASED) {
                release.setVisible(true);
                presed.setVisible(false);
            } else if (type == MouseEvent.MOUSE_MOVED) {
                pos.setX(event.getSceneX());
                pos.setY(event.getSceneY());
                pp.setX(event.getSceneX());
                pp.setY(event.getSceneY());
            }

        });
        int[] lPos = { 137, 272, 406 };
        int[] mPos = { 108, 265, 423 };
        int[] bPos = { 65, 255, 440 };
        int[][] layoutYs = { { 190, 120 }, { 285, 195 }, { 410, 310 } };

        Config.little_mouse.setLayoutX(lPos[0]);
        Config.little_mouse.setLayoutY(layoutYs[0][0]);
        Config.mid_mouse.setLayoutX(mPos[0]);
        Config.mid_mouse.setLayoutY(layoutYs[1][0]);
        Config.big_mouse.setLayoutX(bPos[0]);
        Config.big_mouse.setLayoutY(layoutYs[2][0]);

        timer.schedule(new TimerTask() {

            @Override
            public void run() {

                jump(lPos, layoutYs[0], 0, Config.little_mouse);
                jump(mPos, layoutYs[1], 1, Config.mid_mouse);
                jump(bPos, layoutYs[2], 2, Config.big_mouse);

            }
        }, 100, 50);
    }

    Random    random = new Random();

    boolean[] up     = { true, true, true };

    private void jump(int[] mPos, int[] y, int flagIndex, ImageView mouse) {

        double layoutY = mouse.getLayoutY();

        if (up[flagIndex]) {
            layoutY += 10;
        } else {
            layoutY -= 10;
        }
        if (layoutY < y[1]) {
            up[flagIndex] = true;
        } else if (layoutY > y[0]) {
            up[flagIndex] = false;
            mouse.setLayoutX(mPos[random.nextInt(3)]);
        }
        mouse.setLayoutY(layoutY);
    }

    Timer timer = new Timer();

    @Override
    protected void layoutChildren() {

        final double w = getWidth();
        final double h = getHeight();
        iv[0].setLayoutY(0);
        iv[1].setLayoutY(192);
        iv[2].setLayoutY(192 + 120);
        iv[3].setLayoutY(192 + 120 + 167);
    }

    private ImageView loadbackImage(String path, String sub, double height) throws IOException {

        Image img = Config.loadImage(path);
        if (null != sub) {
            img = Config.getImageOfMask(path, sub);
        }
        ImageView imageView = new ImageView(img);
        imageView.setTranslateY(height);
        return imageView;
    }
}
